<template>
  <div class='recruit-canvas'>
    <div class="btn-box" :style="boxWidth">
      <div class="tip_text">
        <span>请在上方空白处书写您的名字</span>
      </div>
      <div class="but_box">
        <span class="del-btn" @click="clearCanvasHandle">
          <span>清除重写</span>
        </span>
        <span class="sure-btn" @click="makeCanvasHandle">
          <span>提交签字</span>
        </span>
      </div>
    </div>

    <div class="canvas-box" ref="canvasRef" v-show="!previewImage">
      <canvas ref="canvasMapRef" id="canvas-map" width="100" height="100"></canvas>
    </div>
    <!--    <img v-show="previewImage" class="preview-image" :src="previewImage" alt="生成预览">-->
  </div>
</template>

<script>
import SignaturePad from 'signature_pad'
import {rotateBase64Img} from '../../service/common/signName'

export default {
  name: 'signNameCanvas',
  props: ['showSignName'],
  data() {
    return {
      canvasNode: null,
      previewImage: null,
      boxWidth: ''
    }
  },
  mounted() {
    this.initalHandle()
    window.addEventListener('resize', this.initalHandle, false)
  },
  watch: {
    showSignName(val) {
      if (val) {
        this.clearCanvasHandle()
      }
    }
  },
  methods: {
    initalHandle() {
      const _canvasBox = this.$refs.canvasRef
      const _canvas = this.$refs.canvasMapRef
      if (!_canvasBox || !_canvas) {
        return false
      }

      _canvas.width = _canvasBox.clientWidth
      _canvas.height = _canvasBox.clientHeight

      this.clearCanvasHandle()
      this.canvasNode = new SignaturePad(_canvas, {
        minWidth: 2,
        maxWidth: 2,
        penColor: 'rgb(0, 0, 0)'
      })
    },

    clearCanvasHandle() {
      if (this.canvasNode) {
        this.canvasNode.clear()
        this.previewImage = null
      }
    },

    makeCanvasHandle() {
      const canvasNode = this.canvasNode
      // 重新初始化画布
      if (!canvasNode) {
        this.initalHandle()
      }

      // 是否签字
      if (canvasNode.isEmpty()) {
        this.$toast('您还没有签名')
        return false
      }

      // 图像旋转二次处理
      const _boxWidth = window.innerWidth
      const _boxHeight = window.innerHeight
      const _signImg = canvasNode.toDataURL('image/png', 0.6)
      if (_boxWidth < _boxHeight) {
        rotateBase64Img(_signImg, -90, (imgUrlRes) => {
          this.previewImage = imgUrlRes
          this.$emit('preview-image', this.previewImage)
        })
      } else {
        this.previewImage = _signImg
        this.$emit('preview-image', this.previewImage)
      }

      // upload ajax
      // do something here
    }
  },
  beforeDestroy() {
    window.removeEventListener('resize', this.initalHandle, false)
  }
}
</script>

<style lang='less' scoped>
.recruit-canvas {
  position: absolute;
  top: 5.8rem;
  left: 1.2rem;
  bottom: 1.2rem;
  right: 1.2rem;
  z-index: 1000;
  border: 1px solid #E5E6EB;
}

.recruit-canvas .preview-image {
  position: absolute;
  width: 20rem;
  top: 50%;
  left: 50%;
  z-index: 999;
  transform: translate(-50%, -50%);
}

.recruit-canvas .canvas-box
  /*.recruit-canvas .btn-box */ {
  position: absolute;
  top: 50%;
  z-index: 100;
}

.recruit-canvas .btn-box {
  height: 100%;
  width: 6rem;
  background-color: #F7F8FA;
  z-index: 1000;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: space-between;

  .tip_text {
    display: flex;
    align-items: center;
    justify-content: center;
    width: 2rem;
    height: 24rem;
    color: rgb(78, 89, 105);
    font-family: HarmonyOS Sans SC;
    font-size: 16px;
    font-weight: 400;
    text-align: left;

    span {
      white-space: nowrap;
      transform: rotate(90deg);
      -ms-transform: rotate(90deg);
      -moz-transform: rotate(90deg);
      -webkit-transform: rotate(90deg);
      -o-transform: rotate(90deg);
    }
  }

}

.recruit-canvas .btn-box .del-btn,
.recruit-canvas .btn-box .sure-btn {
  border-radius: 2.4rem;
  flex: none;
  order: 1;
  flex-grow: 0;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  width: 4.4rem;
  height: 11rem;
  margin: 1.6rem 0;
  line-height: 11rem;
  text-align: center;
  background-color: #fff;

  span {
    white-space: nowrap;
    transform: rotate(90deg);
    -ms-transform: rotate(90deg);
    -moz-transform: rotate(90deg);
    -webkit-transform: rotate(90deg);
    -o-transform: rotate(90deg);
  }
}

.recruit-canvas .btn-box .del-btn {
  box-sizing: border-box;
  /* Neutral/3深、灰底悬浮 */
  border: 1px solid rgb(229, 230, 235);
  color: rgb(78, 89, 105);
  font-family: HarmonyOS Sans SC;
  font-size: 16px;
  font-weight: 500;
  line-height: 22px;
}

.recruit-canvas .btn-box .sure-btn {
  color: rgb(255, 255, 255);
  font-family: HarmonyOS Sans SC;
  font-size: 16px;
  font-weight: 500;
  line-height: 22px;
  background: linear-gradient(270deg, var(--primary-color), var(--primary50-color));
}


.recruit-canvas .canvas-box {
  height: 100%;
  width: 29rem;
  right: 0;
  border-left: 1px solid #E5E6EB;
  overflow: hidden;
  transform: translateY(-50%);
  background-color: rgba(247, 248, 250, 0.5);
}

.recruit-canvas .canvas-box #canvas-map {
  width: 100%;
  height: 100%;
}


@media screen and (orientation: portrait) {
  /*竖屏 css*/
}

@media screen and (orientation: landscape) {
  /*横屏 css*/
  .recruit-canvas .canvas-box {
    top: 20px;
    left: 10%;
    width: 80vw;
    height: 70vh;
    transform: translateY(0);
  }

  .recruit-canvas .btn-box {
    width: 60%;
    left: 20%;
    top: 86%;
    transform: rotate(0);
  }
}
</style>
